package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.baidu.BaiDuMapUtil;
import com.zw.yzk.kabu.base.BaseApplication;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.ExhibitionListEntity;
import com.zw.yzk.kabu.data.network.task.ExhibitionListTask;
import com.zw.yzk.kabu.data.utils.LogUtils;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.mapper.ExhibitionListMapper;
import com.zw.yzk.kabu.view.view.ExhibitionView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/2/22.
 */

public class ExhibitionPresenter implements Presenter {

    private UseCase getExhibitionList;
    private AppCompatActivity activity;
    private ExhibitionView exhibitionView;
    private ExhibitionListMapper mapper;
    //用于百度定位
    private LocationClient mLocationClient;

    @Inject
    public ExhibitionPresenter(@Named("exhibitionUseCase") UseCase getExhibitionList, AppCompatActivity activity, ExhibitionListMapper mapper) {
        this.activity = activity;
        this.getExhibitionList = getExhibitionList;
        this.mapper = mapper;
    }

    public void setView(ExhibitionView exhibitionView) {
        this.exhibitionView = exhibitionView;
    }

    public void getExhibitionList(int total, String city, String startDate, boolean getLocal) {
        getExhibitionList.execute(new ExhibitionListSubscriber(activity),
                new ExhibitionListTask(total, city, startDate, getLocal));
    }

    /**
     * 开始定位
     */
    public void getLocation() {
        if (mLocationClient != null) {
            mLocationClient.requestLocation();
        }
        mLocationClient = new LocationClient(BaseApplication.getInstance());
        //配置定位设置项
        LocationClientOption option = BaiDuMapUtil.iniBaiDuClientOption();
        //只定位一次,只需要获取一次位置就行
        option.setScanSpan(0);
        mLocationClient.setLocOption(option);
        //注册监听函数
        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                //只在初次进入时定位
                mLocationClient.stop();
                switch (location.getLocType()) {
                    case BDLocation.TypeGpsLocation:
                        //GPS定位结果
                    case BDLocation.TypeNetWorkLocation:
                        //网络定位结果
                    case BDLocation.TypeOffLineLocation:
                        //离线定位结果
                        exhibitionView.setCurrentCity(location.getCity(), true);
                        break;
                    default:
                        exhibitionView.setCurrentCity(activity.getResources().getString(R.string.locate_fail), true);
                        //定位失败
                        break;
                }
            }

            @Override
            public void onConnectHotSpotMessage(String s, int i) {

            }
        });
        mLocationClient.start();
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        if (mLocationClient.isStarted()) {
            mLocationClient.stop();
        }
        getExhibitionList.unSubscribe();
    }

    private class ExhibitionListSubscriber extends BaseSubscriber<ExhibitionListEntity> {

        public ExhibitionListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(ExhibitionListEntity entity) {
            super.onNext(entity);
            exhibitionView.getExhibitionSuccess(mapper.transfer(entity.result.exhibitions));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            exhibitionView.getExhibitionFailed();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_exhibition_list_failed);
        }
    }
}
